// 记住我功能封装
window.rememberMe = {
  set(username, password) {
    const encode = (str) => btoa(unescape(encodeURIComponent(str)));
    const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toUTCString();
    document.cookie = `hrm_username=${encodeURIComponent(username)};expires=${expires};path=/`;
    document.cookie = `hrm_password=${encode(password)};expires=${expires};path=/`;
  },
  clear() {
    document.cookie = 'hrm_username=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
    document.cookie = 'hrm_password=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
  },
  get() {
    function getCookie(name) {
      const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
      return match ? decodeURIComponent(match[2]) : '';
    }
    const username = getCookie('hrm_username');
    const password = getCookie('hrm_password');
    let decodedPassword = '';
    if (password) {
      try {
        decodedPassword = decodeURIComponent(escape(atob(password)));
      } catch(e) {
        decodedPassword = '';
      }
    }
    return {
      username,
      password: decodedPassword
    };
  }
};